package com.yuanqi.dz.dc.service.impl;

import java.util.List;

import pnbclient.helper.StringHelper;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;

import com.powermop.service.info.GetWeChatDzOutInfo;
import com.powermop.service.remote.IRefundInforMation;
import com.yuanqi.dz.dc.util.DZConfigRead;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;

public class WechatDZServiceImpl implements DZDataInterface{
	public static final PnbLogging LOG = PnbLogFactory.getLogger("DZ_EXT_LOG");

	
	public DZInfoArray getDZData(String mercharnum, String dzdate) {
		DZInfoArray dzInfoArray = new DZInfoArray();
		
		String zftx_dz_address = DZConfigRead.getProperties("zftx_dz_address");
		if(StringHelper.isEmpty(zftx_dz_address)){
			dzInfoArray.setRc("-1");
			dzInfoArray.setErrormsg("获取支付体系对账数据HS地址未配置");
			return dzInfoArray;
		}
		LOG.addDebug("create " + IRefundInforMation.class.getSimpleName() + " from:" + zftx_dz_address);
		LOG.addInfo("调用HS接口("+zftx_dz_address+")入参{mercharnum:"+mercharnum+",dzdate:"+dzdate+"}");
		GetWeChatDzOutInfo out = null;
		try {
			
			IRefundInforMation iRefundInforMation = (IRefundInforMation) pnbclient.util.HSClient.create(IRefundInforMation.class, zftx_dz_address);
			out = iRefundInforMation.getWeChatDZData(mercharnum, dzdate);
			
			if(null == out){
				LOG.addError("HS获取微信对账数据为null");
				dzInfoArray = new DZInfoArray();
				dzInfoArray.setRc("-1");
				dzInfoArray.setErrormsg("HS获取微信对账数据为null");
				return dzInfoArray;
			}
			String rc= out.getRc();
			if(null!=rc && "1".equals(rc)){
				List<DZInfo> list = out.getDzlist();
				if(null == list || list.size() ==0){
					LOG.addError("HS获取微信对账数据rc为1，但数据为空");
					dzInfoArray = new DZInfoArray();
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("HS获取微信对账数据为空");
					return dzInfoArray;
				}
				LOG.addInfo("获取微信对账数据成功!数据条数"+list.size());
				dzInfoArray = new DZInfoArray();
				dzInfoArray.setRc("1");
				dzInfoArray.setErrormsg("获取微信对账数据成功!");
				dzInfoArray.setDzInfo(list.toArray(new DZInfo[list.size()]));
			}else{
				LOG.addError(null == out.getErrormsg()?"HS获取微信对账数据失败":out.getErrormsg());
				dzInfoArray = new DZInfoArray();
				dzInfoArray.setRc("-1");
				dzInfoArray.setErrormsg(null == out.getErrormsg()?"HS获取微信对账数据失败":out.getErrormsg());
				return dzInfoArray;
			}
		} catch (Exception e) {
			LOG.addError("HS获取微信对账数据异常"+e);
			dzInfoArray = new DZInfoArray();
			dzInfoArray.setRc("-1");
			dzInfoArray.setErrormsg("HS获取微信对账数据异常");
			return dzInfoArray;
		}
		return dzInfoArray;
		
	}
	public static void main(String[] args) {
		//new WechatDZServiceImpl().getDZData("1412846402", "20170406");
		new WechatDZServiceImpl().getDZData("1285872801", "20170406");
	}
}
